import query from '@dfeidao/atom-nodejs/db/query';
import format from '@dfeidao/atom-nodejs/dt/format';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { systemid } from '../atom/config';

interface Message {
	productid: string;
	teacher_no: string;
	page_no: number;
	// cookie: {
	// 	uk: string;
	// 	[key: string]: string
	// };
	// urls: {
	// 	base: string;
	// 	origin: string;
	// 	url: string;
	// };
	// query: {};
	// params: {};
	// headers: {};
	// captcha: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

interface Data {
	start_time: number;
	start_time_str: string;
	finish_time: number;
	finish_time_str: string;
	auditor_status: string;
	leave_type: string;
	submission_time: number;
	submission_time_str: string;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:app-leave-record/s001,action_id:' + action_id);
	const { exec, prepare } = query(action_id, session_id, systemid);
	prepare('teacher_leave', ['teacher_leave_num', 'teacher_leave_content', 'teacher_no', 'teacher_name', 'leave_type', 'leave_days', 'reviewer', 'auditor_status', 'audit_opinion', 'submission_time', 'productid', 'start_time', 'finish_time'], { productid: message.productid, teacher_no: message.teacher_no }, 5, message.page_no, [], ['submission_time']);
	const [data] = await exec<Data>();
	Array.from(data).map((v) => {
		v.start_time_str = format(v.start_time, 'YYYY-MM-DD aa');
		v.finish_time_str = format(v.finish_time, 'YYYY-MM-DD aa');
		v.submission_time_str = format(v.submission_time, 'YYYY-MM-DD');
		if (v.auditor_status === '0') {
			v.auditor_status = "待审核";
		}
		if (v.auditor_status === '1') {
			v.auditor_status = "已通过";
		}
		if (v.auditor_status === '2') {
			v.auditor_status = "未通过";
		}
		if (v.leave_type === '0') {
			v.leave_type = "事假";
		}
		if (v.leave_type === '1') {
			v.leave_type = "病假";
		}
		if (v.leave_type === '2') {
			v.leave_type = "婚假";
		}
		if (v.leave_type === '3') {
			v.leave_type = "产检假";
		}
	});
	log('Service end path:app-leave-record/s001,action_id:' + action_id);
	return {
		data
	};
}
